package com.sbl.bajw.pc.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.WebContext;
import com.sbl.bajw.mapper.AttFileDao;
import com.sbl.bajw.pc.model.AttFileModel;
import com.sbl.bajw.util.FileUpload;

@Service
public class AttFileService {
	
	@Resource
	AttFileDao dao;
	
	public AttFileModel insert(AttFileModel model) throws Exception{
		
		if(model.getGroupid() == null || model.getGroupid().equals("")){
			throw new MessageException("groupid 不能为空");
		}
		
		if(model.getItem() == null){
			throw new MessageException("fileitem 不能为空");
		}
		
		if(model.getBusinessType() == null || model.getBusinessType().equals("")){
			throw new MessageException("BusinessType 不能为空");
		}
		
		if(model.getFileName() == null || model.getFileName().equals("")){
			throw new MessageException("FileName 不能为空");
		}
		
		if(model.getFileDir() == null || model.getFileDir().equals("")){
			throw new MessageException("FileDir 不能为空");
		}
		
		if(model.getUserid() == null || model.getUserid().equals("")){
			throw new MessageException("Userid 不能为空");
		}
		
		String path = WebContext.getRequest().getSession().getServletContext().getInitParameter("UploadPath");
		String resultPath = "images/upload/"+ model.getFileDir()+ "/" + FileUpload.saveFile(model.getItem(), path + "/images/upload/"+model.getFileDir(), model.getFileName());
		model.setSmallimgurl(resultPath);
		model.setFilesize(model.getItem().getSize() + "");
		dao.insert(model);
		return model;
	}
}
